package com.robot.controller;

import com.robot.constant.Admin;
import com.robot.constant.ScheduleType;
import com.robot.model.TestPlanDetailDo;
import com.robot.model.TestPlanDo;
import com.robot.service.TestPlanService;
import com.robot.util.SessionUtil;
import com.robot.vo.ResponseMessage;
import com.robot.vo.TestPlanBuild;
import com.robot.vo.TreeMenu;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2020/3/22
 * Time: 13:48
 * PROJECT：robot
 * PACKAGE: com.robot
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/testPlan")
@Slf4j
public class TestPlanController {
    @Autowired
    TestPlanService testPlanService;
    @RequestMapping("/testPlanPage")
    public ModelAndView page() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/testplan/plan");
        return modelAndView;
    }
    @RequestMapping("/saveTestPlan")
    public @ResponseBody
    ResponseMessage saveTestPlan(@Valid TestPlanDo testPlanDo, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseMessage.errorResponse(bindingResult.getFieldError().getDefaultMessage());
        }
        Integer scheduleTask =testPlanDo.getScheduleTask();
        if(scheduleTask == ScheduleType.ON){
            String weekTime = testPlanDo.getWeekTime();
            if(StringUtils.isEmpty(weekTime)){
                return ResponseMessage.errorResponse("定时任务【周几】运行需要选择!");
            }
        }
        try {
            String userName = SessionUtil.getSessionUser().getUserName();
            Integer projectId =SessionUtil.getSessionUser().getLastProjectId();
            testPlanDo.setUserName(userName);
            testPlanDo.setProjectId(projectId);
            Integer testPlanId = testPlanDo.getId();
            if (null == testPlanId) {
                testPlanService.addTestPlan(testPlanDo);
            } else {
                testPlanService.updateTestPlan(testPlanDo);
            }
        } catch (Exception e) {
            log.error("增加测试计划异常", e);
            return ResponseMessage.errorResponse("增加测试计划异常,"+Admin.ADMIN_MESSAGE);
        }
        return ResponseMessage.successResponse();
    }
    /**
     * 获取测试计划树
     *
     * @return ResponseMessage
     */
    @RequestMapping("/getPlanTree")
    public @ResponseBody
    ResponseMessage getPlanTree() {
        try {
            Integer projectId = SessionUtil.getSessionUser().getLastProjectId();
            List<TreeMenu> treeList = testPlanService.getTestPlanTree(projectId);
            return ResponseMessage.successResponse(treeList);
        } catch (Exception e) {
            log.error("获取测试计划错误", e);
            return ResponseMessage.errorResponse("获取测试计划异常," + Admin.ADMIN_MESSAGE);
        }
    }
    /**
     * 获取测试计划
     * @param testPlanId
     * @return ResponseMessage
     */
    @RequestMapping("/getTestPlanById")
    public @ResponseBody
    ResponseMessage getTestPlanById(Integer testPlanId) {
        try {
            TestPlanDo testPlanDo = testPlanService.getTestPlanById(testPlanId);
            return ResponseMessage.successResponse(testPlanDo);
        } catch (Exception e) {
            log.error("获取测试计划错误", e);
            return ResponseMessage.errorResponse("获取测试计划异常,"+Admin.ADMIN_MESSAGE);
        }
    }
    /**
     * 获取测试组件
     * **/
    @RequestMapping("/componentListForPlan")
    public @ResponseBody
    ResponseMessage componentListForPlan() {
        return testPlanService.componentListForPlan();
    }
    @RequestMapping("/getComponentAndTestCase")
    public @ResponseBody
    ResponseMessage getComponentAndTestCase(Integer id) {
        try {
            TestPlanBuild testPlanBuild = testPlanService.getComponentAndTestCase(id);
            return ResponseMessage.successResponse(testPlanBuild);
        }catch (Exception e){
            log.error("获取组件异常",e);
            return ResponseMessage.errorResponse("获取组件异常,"+Admin.ADMIN_MESSAGE);
        }
    }
    /**
     * 保存测试计划组件用例信息
     * @param interfaceId
     * @param caseIds
     * @return ResponseMessage
     */
    @RequestMapping("/saveComponentTestCase")
    public @ResponseBody
    ResponseMessage saveComponentTestCase(Integer id,String interfaceAliasName,Integer interfaceId,Integer testPlanId,Integer[] caseIds){
        if(null ==caseIds || caseIds.length ==0){
            return ResponseMessage.errorResponse("请选择要执行的测试用例");
        }
        return testPlanService.saveComponentTestCase(id,interfaceAliasName,interfaceId,testPlanId,caseIds);
    }
    /**
     * 获取测试计划明细
     *
     * @param testPlanDetailId
     * @return ResponseMessage
     */
    @RequestMapping("/getComponentTestCaseById")
    public @ResponseBody
    ResponseMessage getComponentTestCaseById(Integer testPlanDetailId) {
        try {
            TestPlanDetailDo testPlanDetailDo = testPlanService.getComponentTestCaseById(testPlanDetailId);
            return ResponseMessage.successResponse(testPlanDetailDo);
        } catch (Exception e) {
            log.error("获取测试计划错误", e);
            return ResponseMessage.errorResponse("获取测试计划异常,"+Admin.ADMIN_MESSAGE);
        }
    }
    /**
     * 保存场景化测试计划组件用例信息
     * @param testPlanDetailDo
     * @return ResponseMessage
     */
    @RequestMapping("/saveSuiteComponentTestCase")
    public @ResponseBody
    ResponseMessage saveSuiteComponentTestCase(TestPlanDetailDo testPlanDetailDo){
        if(StringUtils.isEmpty(testPlanDetailDo.getParam())){
            return ResponseMessage.errorResponse("入参不能为空");
        }
        return testPlanService.saveSuiteComponentTestCase(testPlanDetailDo);
    }
    /**
     * 删除
     * @param id
     * @return ResponseMessage
     */
    @RequestMapping("/deleteComponentTestCase")
    public @ResponseBody
    ResponseMessage deleteComponentTestCase(Integer id){
        if(null ==id){
            return ResponseMessage.errorResponse("入参不能为空");
        }
        return testPlanService.deleteComponentTestCase(id);
    }
}
